package com.hiq.psycho;

import java.io.*;
import java.util.*;

public class Intellect{
	
	String patternFile0 = "data\\eng\\patterns.txt";
	String saveFile0 = "data\\eng\\save.txt";
	String qaFile0 = "data\\eng\\quickanswers.txt";
	
	String patternFile1 = "data\\rus\\patterns.txt";
	String saveFile1 = "data\\rus\\save.txt";
	String qaFile1 = "data\\rus\\quickanswers.txt";
	
	String notAnsweredFile = "data\\notAnswered.txt";
	
	int mode = 0;
	
	boolean listen = false;

	Map<Word, List<String>> relations = new Hashtable<Word, List<String>>();
	List<String> saveFrases   = new ArrayList<String>();
	List<String> notAnswered  = new ArrayList<String>();
	List<String> quickAnswers = new ArrayList<String>();
	Interlocutor interlocutor;
	
	String previousAnswer;
	
	/** 
		i = 0 - english version
		i = 1 - russian version
	*/
	public Intellect(int i){
		this.mode = i;
		readPatterns();
		readQuickAnswers();
		readSaveFrases();
		interlocutor = new Interlocutor(i);
	}
	
	private void readQuickAnswers(){
		try{
			String f;
			if (mode == 0) {
				f = this.qaFile0; 
			} else {
				f = this.qaFile1;
			}
			
			BufferedReader br = new BufferedReader(new FileReader(f));
			String str;
			while((str = br.readLine()) != null){
				quickAnswers.add(str);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	private void readSaveFrases() {
		try{
			String f;
			if (mode == 0) {
				f = this.saveFile0; 
			} else {
				f = this.saveFile1;
			}
			
			BufferedReader br = new BufferedReader(new FileReader(f));
			String str;
			while((str = br.readLine()) != null){
				saveFrases.add(str);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	private void readPatterns(){
		try{
			String f;
			if (mode == 0) {
				f = this.patternFile0; 
			} else {
				f = this.patternFile1;
			}
			BufferedReader br = new BufferedReader(new FileReader(f));
			String str;
			
			while((str = br.readLine()) != null){
				StringTokenizer st = new StringTokenizer(str, "#");
				String wordsStr = st.nextToken();
				String patternsStr = st.nextToken();
				List<String> localWords = new ArrayList<String>();
				List<String> localPatterns = new ArrayList<String>();
				
				st = new StringTokenizer(wordsStr, "/");
				while(st.hasMoreTokens()){
					localWords.add(st.nextToken());
				}
				
				st = new StringTokenizer(patternsStr, "/");
				while(st.hasMoreTokens()){
					localPatterns.add(st.nextToken());
				}
				
				
				int n = localWords.size();
				
				for (int i=0; i<n; i++){
					relations.put(new Word(localWords.get(i)), localPatterns);
				}
				
			}
			br.close();
			System.out.println(relations.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String answer(String question){
	
		if (listen){
			interlocutor.setParameter(previousAnswer);
			listen = false;
		}
		
		double p = Math.random();
		System.out.println("filled = " + interlocutor.getFilled() + "; total = " + interlocutor.getTotal());
		double s = (double) interlocutor.getFilled() / interlocutor.getTotal();
		System.out.println(p);
		System.out.println(s);
		
		if (p > s){
			listen = true;
			return (this.normalAnswer(question) + " " + interlocutor.getQuestion());
		} else {
			if (question.contains("?")){
				int n = quickAnswers.size();
				int i = (int) Math.floor(Math.random() * n);
				return quickAnswers.get(i);
			} else{
				return normalAnswer(question);
			}
		}
	}
	
	private String normalAnswer(String question){
	
			List<String> words = devideToWords(question);
			List<String> answers = new ArrayList<String>();
			for (String word: words){
				List<String> temp = getPatternsForWord(word);
				if (temp != null){
					answers.addAll(getPatternsForWord(word));
				}
			}
			
			int n = answers.size();
			int i = (int) Math.floor(Math.random() * n);
			
			if (n > 0){
				return answers.get(i);
			} else {
				notAnswered.add(question);
				return saveFrase();
				
			}
	}
	
	private List<String> devideToWords(String str){
		List<String> res = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(str, " .!&@#$%^&*()_+=?,.");
		while(st.hasMoreTokens()){
			res.add(st.nextToken());
		}
		return res;
	}
	
	private List<String> getPatternsForWord(String word){
		Word w = new Word(word);
		return relations.get(w);
	}
	
	private String saveFrase(){
		int n = saveFrases.size();
		int i = (int) Math.floor(n * Math.random());
		return saveFrases.get(i);
	}
	
	protected void finalize(){
		saveNotAsnwered();
	}
	
	private void saveNotAsnwered(){
		try{
			PrintWriter pw = new PrintWriter(notAnsweredFile);
			for (String str: notAnswered){
				pw.println(str);
			}
			pw.flush();
			pw.close();
		} catch(Exception e){
			System.out.println("can't saved not answered questions!");
		}
		
	}
	
	public void setPreviousAnswer(String s){
		this.previousAnswer = s;
	}
}